package com.chuangke.lowcode.component.query;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chuangke.common.trans.annotation.TransResult;
import com.chuangke.common.utils.SpringContextUtil;
import com.chuangke.lowcode.LowCodeContext;
import com.chuangke.lowcode.component.ProgressComponent;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 通用查询组件
 */
@Component
public class PageQueryComponent implements ProgressComponent {

    @Override
    public void process(LowCodeContext modelContext) {
        IService service = (IService) SpringContextUtil.getDaoBean(modelContext.getEntityModel().getEntityClass());

        IPage page = new Page(modelContext.getPage().getCurrent(), modelContext.getPage().getSize()) ;
        IPage result = service.page(page,modelContext.getQueryWrapper()) ;

        //翻译名称映射字段
        ((PageQueryComponent) AopContext.currentProxy()).transResult(result.getRecords()) ;
        modelContext.setResult(result);
    }

    @TransResult
    public List transResult(List datas){
        return datas ;
    }

}
